//
//  GroupAddSPViewController.m
//  sentorise
//
//  Created by admin on 2025/9/12.
//

#import "GroupAddSPViewController.h"
#import "GlobalDefine.h"
#import "ProgressHUD.h"
#import "BlutoothGroupModel.h"
#import "GroupSetSPViewController.h"

@interface GroupAddSPViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topsS;
@property (weak, nonatomic) IBOutlet UITextField *nameTF;
@property (weak, nonatomic) IBOutlet UILabel *numsLabel;
@property (weak, nonatomic) IBOutlet UICollectionView *pCollect;
@property (weak, nonatomic) IBOutlet UICollectionView *sCollect;
@property (weak, nonatomic) IBOutlet UICollectionView *aCollect;

@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UILabel *sysTitleLabel;
@property (weak, nonatomic) IBOutlet UILabel *configTitleLabel;
@property (weak, nonatomic) IBOutlet UILabel *appTItleLabel;
@property (weak, nonatomic) IBOutlet UIButton *submitBtn;


@property (nonatomic, assign) NSInteger pIndex;
@property (nonatomic, assign) NSInteger sIndex;
@property (nonatomic, assign) NSInteger aIndex;

@end

@implementation GroupAddSPViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.topsS.constant = IphoneX ? 54 : 20;
    self.titleLabel.text = NSLocalizedString(@"RV Battery", nil);
    self.sysTitleLabel.text = NSLocalizedString(@"System name*", nil);
    self.configTitleLabel.text = NSLocalizedString(@"System Configuration*", nil);
    self.appTItleLabel.text = NSLocalizedString(@"Application", nil);
    [self.submitBtn setTitle:NSLocalizedString(@"Submit", nil) forState:UIControlStateNormal];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldTextDidChange:) name:UITextFieldTextDidChangeNotification object:self.nameTF];

    [self.pCollect registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"UICollectionViewCell"];
    [self.sCollect registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"UICollectionViewCell"];
    [self.aCollect registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"UICollectionViewCell"];
}

- (IBAction)actionBack:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)actionSave:(id)sender {
    [self.view endEditing:YES];
    if (!self.nameTF.text.length) {
        [ProgressHUD showErrorMessage:NSLocalizedString(@"Please enter system name", nil)];
        return;
    }
    BlutoothGroupModel *model = [[BlutoothGroupModel alloc] init];
    model.groupId = @([[NSDate date] timeIntervalSince1970]).description;
    model.groupName = self.nameTF.text;
    model.s = @(self.sIndex + 1).description;
    model.p = @(self.pIndex + 1).description;
    model.type = @(self.aIndex).description;
    GroupSetSPViewController *vc = [[GroupSetSPViewController alloc] initWithNibName:@"GroupSetSPViewController" bundle:nil];
    vc.model = model;
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)textFieldTextDidChange:(NSNotification *)notification {
    UITextField *textField = (UITextField *)notification.object;
    if ([textField isEqual:self.nameTF]) {
        self.numsLabel.text = [NSString stringWithFormat:@"%ld/20", textField.text.length];
    }
}

#pragma mark - UICollectionViewDelegate
- (NSInteger)collectionView:(nonnull UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if ([collectionView isEqual:self.pCollect]) {
        return 5;
    }else if ([collectionView isEqual:self.sCollect]) {
        return 8;
    }else {
        return 9;
    }
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if ([collectionView isEqual:self.pCollect] || [collectionView isEqual:self.sCollect]) {
        UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"UICollectionViewCell" forIndexPath:indexPath];
        [cell.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
        
        BOOL isSelect = [collectionView isEqual:self.pCollect] ? indexPath.item == self.pIndex : indexPath.item == self.sIndex;
        UIView *view = [[UIView alloc] initWithFrame:cell.contentView.bounds];
        view.backgroundColor = isSelect ? RGB16(0x68B82E) : [UIColor whiteColor];
        view.layer.cornerRadius = 16;
        [cell.contentView addSubview:view];
        
        UILabel *label = [[UILabel alloc] initWithFrame:view.bounds];
        label.text = @(indexPath.item + 1).description;
        label.textColor = isSelect ? [UIColor whiteColor] : RGB16(0x222222);
        label.textAlignment = NSTextAlignmentCenter;
        label.font = [UIFont systemFontOfSize:18 weight:UIFontWeightMedium];
        [view addSubview:label];
        
        return cell;
    }else {
        UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"UICollectionViewCell" forIndexPath:indexPath];
        [cell.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
        
        BOOL isSelect = indexPath.item == self.aIndex;
        UIView *view = [[UIView alloc] initWithFrame:cell.contentView.bounds];
        view.backgroundColor = isSelect ? RGB16(0x68B82E) : [UIColor whiteColor];
        view.layer.cornerRadius = 16;
        [cell.contentView addSubview:view];
        
        CGFloat w = (kWidth - 40 - 24) / 3;
        UIImageView *iconIV = [[UIImageView alloc] initWithFrame:CGRectMake((w - 56) / 2, 14, 56, 56)];
        iconIV.image = [UIImage imageNamed:[NSString stringWithFormat:@"system_app_type_%ld", indexPath.item]];
        [cell.contentView addSubview:iconIV];
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 70, w, w - 70)];
        NSString *type = @[@"RV & Van", @"Mower", @"Boat", @"Golf Cart", @"Electric\nwheelchair", @"ATV", @"Off-grid Cabin", @"household", @"Others"][indexPath.item];
        label.text = NSLocalizedString(type, nil);
        label.textColor = isSelect ? [UIColor whiteColor] : RGB16(0x222222);
        label.textAlignment = NSTextAlignmentCenter;
        label.font = [UIFont systemFontOfSize:12];
        label.numberOfLines = 0;
        [view addSubview:label];

        return cell;
    }
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    if ([collectionView isEqual:self.pCollect] || [collectionView isEqual:self.sCollect]) {
        return CGSizeMake(50, 60);
    }else {
        return CGSizeMake((kWidth - 40 - 24) / 3 - 1, (kWidth - 40 - 24) / 3);
    }
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    if ([collectionView isEqual:self.pCollect] || [collectionView isEqual:self.sCollect]) {
        return 4;
    }else {
        return 12;
    }
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    if ([collectionView isEqual:self.pCollect] || [collectionView isEqual:self.sCollect]) {
        return 0;
    }else {
        return 12;
    }
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if ([collectionView isEqual:self.pCollect]) {
        self.pIndex = indexPath.item;
        if ((indexPath.item + 1) * (self.sIndex + 1) > 16) {
            self.sIndex = 0;
            [self.sCollect reloadData];
        }
        [self.pCollect reloadData];
    }else if ([collectionView isEqual:self.sCollect]) {
        self.sIndex = indexPath.item;
        if ((indexPath.item + 1) * (self.pIndex + 1) > 16) {
            self.pIndex = 0;
            [self.pCollect reloadData];
        }
        [self.sCollect reloadData];
    }else {
        self.aIndex = indexPath.item;
        [self.aCollect reloadData];
    }
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    return [textField resignFirstResponder];
}


@end
